jQuery.noConflict();
jQuery(document).ready(function () {
	jQuery("input[type=checkbox]").change(function (){
//		console.log("check change" + jQuery(this).val());
		if (jQuery(this).is(":checked")){
			jQuery(this).val(1);           
		} else {
			jQuery(this).val(0);           
		}
	});
});

// sleep ms
function sleep(numberMillis) {
	var now = new Date();
	var endTime = now.getTime() + numberMillis;
	while(true) {
		now = new Date();
		console.log(now.getTime() + " --> " + endTime);
		if(now.getTime() > endTime ) {
			return false;
		}
	} 
}

var redis = {
//产品单元选择弹层
	showCpunit : function () {
		var id = jQuery("#cpunit_id").val();
		if (!id) id = 0;
		
		show_float_box("?m=node&a=showCpunit", function () {}, 500);
		//document.getElementById('cpunit_id').value = index;
		setTimeout(function () {
			showCpunitInfo(id);
		}, 500);
		var id_new = jQuery("#cpunit_id").val();
	},

	showStatusImage: function(flag) {
		var yes = '<img src="static/image/yes.gif" title="成功">';
		var no = '<img src="static/image/no.gif" title="失败">';
		var minus = '<img src="static/image/minus.gif" >';
		switch(flag) {
			case 0:
				return no;
			case 1:
				return yes;
			case 2:
				return minus;
		}
	}
}

//选定产品单元id
function chooseCpunit() {
	//console.log('showc');
	var _html = '';
	var _cid = null;
	var c1 = jQuery("#cpunitlv1 a[class='sel']");
	if (c1.length == 1) { 
		_html += c1.html();
		_cid = c1.attr('id').substr(4);
	}
	var c2 = jQuery("#cpunitlv2 a[class='sel']");
	if (c2.length == 1) {
		_html += '/' + c2.html();
		_cid = c2.attr('id').substr(4);
	}
	var c3 = jQuery("#cpunitlv3 a[class='sel']");
	if (c3.length == 1) {
		_html += '/' + c3.html();
		_cid = c3.attr('id').substr(4);
	}
	var c4 = jQuery("#cpunitlv4 a[class='sel']");
	if (c4.length == 1) {
		_html += '/' + c4.html();
		_cid = c4.attr('id').substr(4);
	}
	jQuery("#showCpunit").val(_html);
	jQuery("#cpunit_id").val(_cid);
	close_float_box();
}
//产品单元数据显示
function showCpunitInfo(id) {
//showCpunitInfo : function (id) {
	jQuery.getJSON("?m=node&a=cpunitInfo&id=" + id, function (obj) {
		if(obj) {
			var _hmlt1 = _html2 = _html3 = _html4 = '';
			if (obj.lv1) {
				_html1 = '<ul>';
				for(var i = 0; i < obj.lv1.length; i++) {
					_html1 += '<li><a id="cid_' + obj.lv1[i].id + '" href="javascript:void(0);" ';
					if (obj.lv1_id == 0) {
						if (obj.id == obj.lv1[i].id) {
							_html1 += ' class="sel" ';
						}
					} else {
						if (obj.lv1_id == obj.lv1[i].id) {
							_html1 += ' class="sel" ';
						}
					}
					_html1 += ' onclick="showCpunitInfo(' + obj.lv1[i].id + ');">' + obj.lv1[i].name + '</a></li>';
				}
				_html1 += '</ul>';
			}
			if (obj.lv2) {
				_html2 = '<ul>';
				for(var i = 0; i < obj.lv2.length; i++) {
					_html2 += '<li><a id="cid_' + obj.lv2[i].id + '" href="javascript:void(0);" ';
					if (obj.lv2_id == obj.lv2[i].id || obj.id == obj.lv2[i].id) {
						_html2 += ' class="sel" ';
					}
					_html2 += ' onclick="showCpunitInfo(' + obj.lv2[i].id + ');">' + obj.lv2[i].name + '</a></li>';
				}
				_html2 += '</ul>';
			}
			if (obj.lv3) {
				_html3 = '<ul>';
				for(var i = 0; i < obj.lv3.length; i++) {
					_html3 += '<li><a id="cid_' + obj.lv3[i].id + '" href="javascript:void(0);" ';
					if (obj.lv3_id == obj.lv3[i].id || obj.id == obj.lv3[i].id) {
						_html3 += ' class="sel" ';
					}
					_html3 += ' onclick="showCpunitInfo(' + obj.lv3[i].id + ');">' + obj.lv3[i].name + '</a></li>';
				}
				_html3 += '</ul>';
			}
			if (obj.lv4) {
				_html4 = '<ul>';
				for(var i = 0; i < obj.lv4.length; i++) {
					_html4 += '<li><a id="cid_' + obj.lv4[i].id + '" href="javascript:void(0);" ';
					if (obj.lv4_id == obj.lv4[i].id || obj.id == obj.lv4[i].id) {
						_html4 += 'class="sel" ';
					}
					_html4 += ' onclick="showCpunitInfo(' + obj.lv4[i].id + ');">' + obj.lv4[i].name + '</a></li>';
				}
				_html4 += '</ul>';
			}
			jQuery("#cpunitlv1").html(_html1);
			jQuery("#cpunitlv2").html(_html2);
			jQuery("#cpunitlv3").html(_html3);
			jQuery("#cpunitlv4").html(_html4);
		}
	});
}
